home *** CD-ROM | disk | FTP | other *** search
- Path: tokyonet.ad.jp!wincgw1!senri-nc!odins-suita!aist-nara!wnoc-kyo-news!kuis-news!shamada
- From: shamada@kuis.kyoto-u.ac.jp (Shin-ichirou Hamada)
- Newsgroups: comp.lang.c++
- Subject: Re: newbie needs help
- Date: 20 Mar 1996 05:04:23 GMT
- Organization: Dept. of Information Science, Kyoto University, JAPAN
- Distribution: world
- Message-ID: <4io3kn$4gq@hemp.imel.kyoto-u.ac.jp>
- References: <$sjBUKACjrSxEw4g@waichung.demon.co.uk>
- NNTP-Posting-Host: goofy
- X-Newsreader: mnews [version 1.19] 1995-07/21(Fri)
- Originator: news@aladdin
-
- In the article, newbie needs help,
- kyn@waichung.demon.co.uk said as ">..."
-
- Hello, My name is HAMADA Shin-ichirou.
- I am poor at English, but I want to do something to aid you( and want
- to talk with English-speakers), so I'm going to post this article.
-
- #I'm sorry if there are any deviant representation.
-
- >I require help in constructing a binary search tree in c++, as i am
- >relatively new to the language i am not sure how to go about
- >implementing it.
-
- I am also C++ beginner, but tries to think this subject on C++ together.
-
- > struct node
- > {
- > data d;
- > struct node *left;
- > struct node *right;
- > }
-
- >if this is correct how do build a tree from this and insert words into
- >it?
-
- At first, let's divide this subject into class-user side and
- class-designing side to analyze requirement.
-
- By later side, it is happy that a binary-tree is just like only an
- object (This course is same as list structure), which we call as
- btree-container class, in which entry and delete(Oh, this is needless,
- isn't it?) method of a node are prepared(Some methods for reading are
- necessary, but they are out of discussion in this time). These methods
- construct binary tree automatically.
-
- Then, let's think about class designing on the basis of above points.
- we prepare two classes, that is to say, btree container class and
- btree node class.
-
- Following is a sample of the classes;
-
- class BTREE {
- private:
- BT_NODE *root_node;
- public:
- // constructor
- BTREE() : root_node( NULL ){
- }
- // destructor
- ~BTREE(){
- delete root_node;
- }
- // entry method
- void entry( DATA data ){
- if( root_node == NULL ){
- root_node = new BT_NODE( data );
- } else {
- root_node->add( data );
- }
- }
- }
-
- class BT_NODE {
- private:
- BT_NODE *left_node;
- BT_NODE *right_node;
- DATA _data;
- cmp_data( DATA target, DATA current );
- // private constructor
- BT_NODE( DATA data )
- : left_node( NULL ), right_node( NULL ), _data( data ) {
- }
- // private destructor
- virtual ~BT_NODE(){
- delete left_node;
- delete right_node;
- }
- public:
- // recursive entry method
- BT_NODE *add( DATA data ){
- // You can make cmp_data() as you like.
- // In this time, allowing duplication, no handle of zero.
- // left side <= right side
- if( cmp_data( data, _data ) < 0 ){
- if( left_node != NULL ){
- left_node->add( data );
- } else {
- left_node = new BT_NODE( data );
- }
- } else {
- if( right_node != NULL ){
- right_node->add( data );
- } else {
- right_node = new BT_NODE( data );
- }
- }
- }
-
- Following is a sample usage;
-
- int main()
- {
- BTREE btree;
- DATA dat1;
- DATA dat2;
- DATA dat3;
-
- btree.entry( dat1 );
- btree.entry( dat2 );
- btree.entry( dat3 );
- }
-
- I explain above program.
- You can make binery-tree if you only send a message "entry( dat )" to
- an BTREE object..Perhaps(^^;
-
- At first, this message reach BTREE object, then if root_node is NULL,
- the data are entered on root_node and processing is completed. If
- root_node isn't NULL, a message "add( dat )" are sent to root_node.
- If a node object receive this message, he compare dat in the massage
- with his dat, and will pass the message to the left( or right ) side
- node object, when if destination object doesn't exist( that is to say,
- left( or right ) side pointer is NULL ), the dat will be registered as
- a new node there.
-
- >The following is how i would implement a binary search tree in pascal.
- >Your help will be much appreciated.
- >BTW i mostly program in pascal but i felt that i needed to learn other
- >languages.
- #what is BTW??
- Sorry, I am not well acquainted with Pascal.
-
- Good bye.
-
-
- --
-
- \----%@ Doushita Lab., Information Science Cource,
- \ / Technology Department, Kyoto Univ.
- \/
- $B!C(B HAMADA, Shin-ichirou
- $B!1(B
-